package cn.tedu.boot051.controller;

import cn.tedu.boot051.entity.User;
import cn.tedu.boot051.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;
    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
        User u = mapper.selectPassword(user.getUsername());
        if (u!=null){
            return 1;
        }else {
            mapper.insert(user);
            return 2;
        }
    }

    @RequestMapping("/check")
    public String check(String username){
        User u = mapper.selectPassword(username);
        return u!=null?"用户名已存在!":"用户名可用!";
    }

    @RequestMapping("/login")
    public Integer login(@RequestBody User user){
        User u = mapper.selectPassword(user.getUsername());
        if (u==null){
            if (u.getPassword().equals(user.getPassword())){
                return 1;
            }
            return 2;
        }
        return 3;
    }

    @RequestMapping("/check2")
    public String check2(String username){
        User u = mapper.selectPassword(username);
        return u!=null?"用户名存在!":"用户名不存在!";
    }

}
